<?php
 
 global $_GPC, $_W;
        $uniacid = $this->_uniacid;
        $uid = $_W['member']['uid'];
        $openid = $_W['openid'];
        /**/
        $search['keyword'] = trim($_GPC['keyword']);
        $search['area'] = trim($_GPC['area']);
        $search['minprice'] = intval($_GPC['minprice']);
        $search['maxprice'] = intval($_GPC['maxprice']);
        $search['minsufix'] = intval($_GPC['minsufix']);
        $search['maxsufix'] = intval($_GPC['maxsufix']);
        $search['room'] = intval($_GPC['room']);
        $search['isjingping'] = intval($_GPC['isjingping']);
        $search['istop'] = intval($_GPC['istop']);
        $search['village_id'] = intval($_GPC['village_id']);
        $search['order'] = intval($_GPC['order']);
        $search['morefind'] = $_GPC['morefind'];
        $zhutype = intval($_GPC['zhutype']);
        $rent_type = intval($_GPC['rent_type']);
        if (isset($_GPC['rent_type'])) {
            $rent_type = intval($_GPC['rent_type']);
        }
        /*不区分房源类型*/
        if(isset($_GPC['notype']) && $_GPC['notype']==1){
             $condition = " uniacid='$uniacid' and isonline=1 and isdelete=0 ";
             if(isset($_GPC['zhutype'])){
                  $condition .= "  and zhutype='$zhutype' ";
             }
             if(isset($_GPC['rent_type'])){
                  $condition .= "  and rent_type='$rent_type' ";
             }
        }else{ /*默认类型*/
             $condition = " uniacid='$uniacid' and isonline=1 and isdelete=0 and rent_type='$rent_type' and zhutype='$zhutype' ";
        }  
       
        /*多城市切换*/
       if($this->module['config']['isduocity']==1  ){
             $default_city = pdo_fetchcolumn("select conf_value from ".tablename("kbwdl_config")." where uniacid='$uniacid' and placeid='800'");  
               /*查询个人设置的*/
                    $ucity = pdo_fetchcolumn("select city_id from ".tablename("kbwdl_share_user")." where uniacid='$uniacid' and openid='$openid'");
                    if($ucity){
                        $default_city = $ucity;
                    }
             $default_city = intval($default_city);
             if($default_city>0){
                $condition .=" and city_id = '$default_city' ";
             }
         }
 
        /**/        
        /* 指定bid 表示查询经纪人的房源，isbroker =1 通过openid查询房源 这个是前端展示，不用于管理房源 */
        if (isset($_GPC['isbroker']) && $_GPC['isbroker'] == 1) {
            $brokerid = intval($_GPC['bid']);
            if ($brokerid > 0) {
                $condition = " uniacid='$uniacid' and broker_id='$brokerid' and rent_type='$rent_type' and zhutype in(0,1,2,3,7,8) and isonline=1 and isdelete=0 ";
            } else {
                $brokerid = pdo_fetchcolumn("select id from " . tablename("kbwdl_broker") . " where openid='$openid' and uniacid='$uniacid'");
                if ($brokerid > 0) {
                    $condition = " uniacid='$uniacid' and broker_id='$brokerid' and rent_type='$rent_type' and isonline=1 and isdelete=0  and zhutype in(0,1,2,3,7,8) ";
                } else {
                    $condition = " uniacid='$uniacid' and broker_name='$openid' and rent_type='$rent_type' and isonline=1 and isdelete=0  and zhutype in(0,1,2,3,7,8) ";
                }
            }
        }
        /* 分页参数 */
        $page = max(1, $_GPC['page']);
        $pagesize = 10;
        $startlimit = ($page - 1) * $pagesize;

        if (!empty($search['keyword'])) {
            $condition .= " and (`title` like '%{$search['keyword']}%' or `area` like '%{$search['keyword']}%' or `village_name` like '%{$search['keyword']}%')";
        }
        if (!empty($search['area'])) {
            $condition .= " and area='{$search['area']}'";
        }
        if (!empty($search['minprice'])) {
            $condition .= " and loyer>='{$search['minprice']}'";
        }
        if (!empty($search['maxprice'])) {
            $condition .= " and loyer<='{$search['maxprice']}'";
        }
        if (!empty($search['minsufix'])) {
            $condition .= " and superficie>='{$search['minsufix']}'";
        }
        if (!empty($search['maxsufix'])) {
            $condition .= " and superficie<='{$search['maxsufix']}'";
        }
        if (!empty($search['village_id'])) {
            $condition .= " and village_id='{$search['village_id']}'";
        }
       /* if (!empty($search['room'])) {
            $condition .= " and room='{$search['room']}'";
        }*/
        if ($search['isjingping'] == 1) {
            $condition .= " and isjingpin='{$search['isjingping']}'";
        }
        /**
         * 处理跟多搜索的情况
         */
        if(isset($_GPC['morefind']) && !empty($_GPC['morefind'])){
            $morefind = explode(',', trim(trim($_GPC['morefind'],']'), '[') );
            foreach($morefind as $k => $v){
                $morefind[$k] = trim($v,'&quot;');
            }
            /*户型*/
            if($morefind[0]){
                $condition .= " and room='{$morefind[0]}'";
            }/*类型 高层*/
            if($morefind[1]){
                $condition .= " and house_type='{$morefind[1]}'";
            }/*装修*/
            if($morefind[2]){
                $condition .= " and traveaux_finition='{$morefind[2]}'";
            }/*房龄*/
            if($morefind[3]){
                $years = date('Y');
                $tmp = array(
                    '1'=> " and ( build_age  <".$years." and build_age>".($years-2)." )",
                    '2'=> " and (build_age   <".($years-2)." and build_age>".($years-5).")",
                    '3'=> " and ( build_age   <".($years-5)." and build_age>".($years-10).")",
                    '4'=> " and ( build_age   <".($years-10)." and build_age>1980 )",
                );
                $condition .=  $tmp[$morefind[3]];
            }/*发布类型*/
            if($morefind[4]){
                if($morefind[4]==1){
                     $condition .= " and ptype='1' ";
                }else{
                    $condition .= " and ptype='0' "; 
                }
                
            }
            /*租房的类型*/
            if($morefind[5]>0){
                if($morefind[5]==1){
                     $condition .= " and danyuan='整租' ";
                }elseif($morefind[5]==2){
                    $condition .= " and danyuan='合租' "; 
                }else{}                
            } 
            if($morefind[6]>0){
                if($morefind[6]==1){
                     $condition .= " and dong='年租' ";
                }elseif($morefind[6]==2){
                    $condition .= " and dong='月租' "; 
                }elseif($morefind[6]==3){
                    $condition .= " and dong='日租' "; 
                }else{}                
            } 
            /*7 楼层*/
            if(isset($morefind[7]) && !empty($morefind[7])){
                $tmp = array(
                    '1-5'=> " and ( storey  < 6 and storey> 0 )",
                    '5-15'=> " and (storey   < 16 and storey> 5)",
                    '15-25'=> " and ( storey   < 26 and storey >15 )",
                    '25-35'=> " and ( storey   <200  and storey >25 )",
                );
               $condition .=  $tmp[$morefind[7]];
            }
            /*厂房和仓库 搜索 结构和 新旧程度*/
             if($morefind[8]){
                $condition .= " and danyuan='{$morefind[8]}'";
            }/*结构*/
             if($morefind[9]){
                $condition .= " and dong='{$morefind[9]}'";
            }/*新旧程度*/
        } 
        /*排序*/       
        if($search['order']>0){
            $tmp = array(1=>" add_time desc ", 2=> ' update_time desc ', 3=>' loyer desc');
            $order = $tmp[$search['order']];            
        }
        if(empty($order)){
             $order = "refresh_time desc, update_time desc";
        }
        /* 置顶设置 */
        if ($search['istop'] == 1) {
            $condition .= " and istop='1' ";
            $pagesize = 1000;
        } else {
            if (!isset($_GPC['isbroker'])) {
                $condition .= " and istop='0' ";
            }
        }
     /*按当前用户类型 查看*/
        $permission = " and permission=0 ";
        $groupid = 0;
        if(!empty($openid)){
             $groupid = pdo_fetchcolumn("select groupid from ".tablename('kbwdl_broker')." where  uniacid='$uniacid' and openid='$openid'");
             if($groupid>0){
                 /*经纪人可以查看 permission in (0,1)*/
                 $permission = " and permission < 2 ";
             }
        }        
        
        $fileds =  $this->_query_sechouse_field();

        $sql = "SELECT $fileds  FROM " . tablename("kbwdl_house") . " where $condition  $permission order by $order limit $startlimit, $pagesize";
        $items = pdo_fetchall($sql);
   
        /**
          * 查询是否分销
          */
        $ischarges =   0;
        $ret2 = pdo_fetch("select * from ".  tablename("kbwdl_config")." where uniacid='$uniacid' and placeid=91 and module='kbwdl_shequzufang'");
        if(isset($ret2['conf_value']) && $ret2['conf_value']==1){
           $ischarges =  1;           
           /*查询经纪人状态*/          
           if(!empty($groupid)){
               $groupid = 1;
           }
        }        
        if (!empty($items)) {
            foreach ($items as $key => $item) {
               
                $val = $this->_format_sechouse($item);
                 /*经纪人*/
                    $val['groupid'] = 0;
                    if($val['iscompany']==9){
                        $val['pushtype'] = '个人';
                    }else{
                        $val['pushtype'] = '中介';
                    }
                    if($val['broker_id']>0){
                        $val['groupid'] = pdo_fetchcolumn("select groupid from ".tablename("kbwdl_broker"). " where id='".$val['broker_id']."'");
                        $val['pushtype'] = '经纪人';
                        if($val['groupid']==0){
                            $val['pushtype'] = '个人';
                        }
                    }  
                    /*求租求购获取发布人头像*/
                    if($val['zhutype']==4){
                        $tmp = pdo_fetch("select nickname,avatar from ".tablename("kbwdl_broker"). " where id='".$val['broker_id']."'");
                        if($tmp['avatar']=='undefined' || empty($tmp['avatar'])){
                            $tmp['avatar'] = 'addons/kbwdl_shequzufang/style/images/house_face.jpg';
                        }
                        $tmp['avatar'] = toimage($tmp['avatar']);
                        $val['sendbroker'] = $tmp;
                         
                    }
               
               $items[$key] = $this->_format_show_item($val, $ischarges, $groupid) ;
             }
        }
     
        /*新增加可以获取总数*/       
        if(isset($_GPC['gettotal']) && $_GPC['gettotal']==1){
             $data['total'] = pdo_fetchcolumn(" select count(*)  from ". tablename("kbwdl_house") . " where $condition ");
             $data['items'] = $items;
             $data['page'] = $page;
             $this->result(0, "查询成功 total", $data);

        }else{
           
            $this->result(0, "查询成功 $sql", $items); 
        }
        
       /* } else {
            $this->result(0, "$sql", $items);
        }
        */
        
?>
